#include <stdlib.h>
#include <math.h>
#include "Object.h"

Object::Object(TextureManager * textureManager, VertexList * vertices)
{
	this->textureManager = textureManager;
	this->vertices = vertices;
	activeObject = false;
	material = NULL;
	textureIndex = -1;
	textureRotation = 0.0;
	propertyMenu = NULL;
}
Object::~Object()
{
	if(vertices)
	{
		delete vertices;
		vertices = NULL;
	}
	if(material)
	{
		delete material;
		material = NULL;
	}
	if(propertyMenu)
	{
		delete propertyMenu;
		propertyMenu = NULL;
	}
}
void Object::ViewProperties()
{
}
void Object::HideProperties()
{
}
void Object::RotateTexture(float theta)
{
	textureRotation += theta;
}

Material * Object::GetMaterial()
{
	return material;
}
void Object::SetMaterial(Material * material)
{
	this->material = material;
	if(textureManager)
	{
		if(!textureManager->LoadTexture(material->fileName))
		{
			printf("Failed to load %s\n", material->fileName);	
		}
		else
			textureIndex = textureManager->TextureIndex(material->fileName);
	}
}
void Object::AddVertex(Vertex * vertex)
{
	if(vertices)
		vertices->Add(vertex);
	else
		vertices = new VertexList(vertex);
}
void Object::AddVertexBefore(Vertex * vertex, Vertex * after)
{
	if(vertices && after && vertex)
		vertices->AddBefore(vertex, after);
}
void Object::RemoveVertex(Vertex * vertex)
{
	if(vertices)
		vertices->Remove(vertex);
}
void Object::Draw()
{
	if(vertices)
		vertices->Draw();
}
void Object::TexturedDraw()
{
}
void Object::GetHoverGroup(Mouse * mouse, VertexList * group)
{
	if(! group)
		return;
	Vertex * left = NULL;
	Vertex * right = NULL;
	if(vertices)
		vertices->GetHoverSegment(mouse, &left, &right);
		
	if(left && right)
	{
		group->Add(left);
		group->Add(right);
	}
}
void Object::CheckHover(Mouse * mouse)
{
	if(vertices)
		vertices->HoverCheck(mouse);
}	
Vertex * Object::GetHover(Mouse * mouse)
{
	if(vertices)
	{
		return vertices->GetHoverVertex(mouse);
	}
	return NULL;	
}
bool Object::CheckObjectHover(Mouse * mouse)
{
	Vertex * vertex = vertices->GetHoverVertex(mouse);
	
	if(vertex)
	{
		activeObject = true;
	}
	else
		activeObject = false;
	return activeObject;	
}
char * Object::GetTag()
{
	return vertices->GetTag();
}
Vector2D Object::GetRotationPoint()
{
	return Vector2D(0,0);
}
VertexList * Object::GetVertices()
{
	return vertices;
}
void Object::Rotate(float theta)
{
	float sinTheta = sin(theta);
	float cosTheta = cos(theta);
	List<Vertex> * search = vertices->head;
	
	while(search)
	{
		Vector2D vertex = search->payload->GetPosition() - GetRotationPoint();
		float newX = vertex.x * cosTheta - vertex.y * sinTheta;
		float newY = vertex.y * cosTheta + vertex.x * sinTheta;
		Vector2D newPos = Vector2D(newX, newY) + GetRotationPoint();
		search->payload->SetPosition(newPos);
		search = search->next;
	}	
}
void Object::Expand(float ratio)
{
	List<Vertex> * search = vertices->head;
	
	while(search)
	{
		if(search->payload)
		{
			Vector2D dir = search->payload->GetPosition() - GetRotationPoint();
			float dist = dir.Magnitude();
			dir = !dir * dist * ratio;
			search->payload->SetPosition(GetRotationPoint() + dir);
		}	
		search = search->next;
	}	
}
void Object::CalculatePosition()
{
	
}
void Object::SetMin(int min)
{
	vertices->SetMin(min);
}
void Object::SetMax(int max)
{
	vertices->SetMax(max);
}
Object * Object::GetParent()
{
	return NULL;
}